/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.settlement.service.customersettlement;

import java.util.Date;
import java.util.List;

import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.settlement.entity.investorsettlement.InvestorSettlement;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.settlement.entity.customersettlement.CustomerSettlement;
import com.jeesite.modules.settlement.dao.customersettlement.CustomerSettlementDao;
import com.jeesite.modules.settlement.entity.customersettlement.CustomerSettlementPayment;
import com.jeesite.modules.settlement.dao.customersettlement.CustomerSettlementPaymentDao;
import com.jeesite.modules.settlement.entity.customersettlement.CustomerSettlementPrepayment;
import com.jeesite.modules.settlement.dao.customersettlement.CustomerSettlementPrepaymentDao;
import com.jeesite.modules.settlement.entity.customersettlement.CustomerSettlementDetail;
import com.jeesite.modules.settlement.dao.customersettlement.CustomerSettlementDetailDao;

/**
 * 客户结算单Service
 * @author Kilo
 * @version 2021-03-10
 */
@Service
@Transactional(readOnly=true)
public class CustomerSettlementService extends CrudService<CustomerSettlementDao, CustomerSettlement> {

	@Autowired
	private CustomerSettlementPaymentDao customerSettlementPaymentDao;

	@Autowired
	private CustomerSettlementPrepaymentDao customerSettlementPrepaymentDao;

	@Autowired
	private CustomerSettlementDetailDao customerSettlementDetailDao;

	@Autowired
	private CodeRuleService codeRuleService;

	/**
	 * 获取单条数据
	 * @param customerSettlement
	 * @return
	 */
	@Override
	public CustomerSettlement get(CustomerSettlement customerSettlement) {
		CustomerSettlement entity = super.get(customerSettlement);
		if (entity != null){
			CustomerSettlementPayment customerSettlementPayment = new CustomerSettlementPayment(entity);
			customerSettlementPayment.setStatus(CustomerSettlementPayment.STATUS_NORMAL);
			entity.setCustomerSettlementPaymentList(customerSettlementPaymentDao.findList(customerSettlementPayment));
			CustomerSettlementPrepayment customerSettlementPrepayment = new CustomerSettlementPrepayment(entity);
			customerSettlementPrepayment.setStatus(CustomerSettlementPrepayment.STATUS_NORMAL);
			entity.setCustomerSettlementPrepaymentList(customerSettlementPrepaymentDao.findList(customerSettlementPrepayment));
			CustomerSettlementDetail customerSettlementDetail = new CustomerSettlementDetail(entity);
			customerSettlementDetail.setStatus(CustomerSettlementDetail.STATUS_NORMAL);
			entity.setCustomerSettlementDetailList(customerSettlementDetailDao.findList(customerSettlementDetail));
		}
		return entity;
	}

	/**
	 * 查询分页数据
	 * @param customerSettlement 查询条件
	 * @param customerSettlement.page 分页对象
	 * @return
	 */
	@Override
	public Page<CustomerSettlement> findPage(CustomerSettlement customerSettlement) {
		return super.findPage(customerSettlement);
	}

	@Transactional(readOnly=false)
	public void saveFinish(CustomerSettlement customerSettlement){
		customerSettlement.setUpdateBy(UserUtils.getUser().getLoginCode());
		customerSettlement.setUpdateDate(new Date());
		super.save(customerSettlement);
	}

	/**
	 * 保存数据（插入或更新）
	 * @param customerSettlement
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(CustomerSettlement customerSettlement) {
		if(customerSettlement.getIsNewRecord()){
			customerSettlement.setCustomerSettlementCode(codeRuleService.getCode("CustomerSettlement"));
			customerSettlement.setCreateBy(UserUtils.getUser().getLoginCode());
			customerSettlement.setCreateDate(new Date());
		}
		super.save(customerSettlement);
		// 保存 CustomerSettlement子表
		for (CustomerSettlementPayment customerSettlementPayment : customerSettlement.getCustomerSettlementPaymentList()){
			if (!CustomerSettlementPayment.STATUS_DELETE.equals(customerSettlementPayment.getStatus())){
				customerSettlementPayment.setCustomerSettlementId(customerSettlement);
				if (customerSettlementPayment.getIsNewRecord()){
					customerSettlementPaymentDao.insert(customerSettlementPayment);
				}else{
					customerSettlementPaymentDao.update(customerSettlementPayment);
				}
			}else{
				customerSettlementPaymentDao.delete(customerSettlementPayment);
			}
		}
		// 保存 CustomerSettlement子表
		for (CustomerSettlementPrepayment customerSettlementPrepayment : customerSettlement.getCustomerSettlementPrepaymentList()){
			if (!CustomerSettlementPrepayment.STATUS_DELETE.equals(customerSettlementPrepayment.getStatus())){
				customerSettlementPrepayment.setCustomerSettlementId(customerSettlement);
				if (customerSettlementPrepayment.getIsNewRecord()){
					customerSettlementPrepaymentDao.insert(customerSettlementPrepayment);
				}else{
					customerSettlementPrepaymentDao.update(customerSettlementPrepayment);
				}
			}else{
				customerSettlementPrepaymentDao.delete(customerSettlementPrepayment);
			}
		}
		// 保存 CustomerSettlement子表
		for (CustomerSettlementDetail customerSettlementDetail : customerSettlement.getCustomerSettlementDetailList()){
			if (!CustomerSettlementDetail.STATUS_DELETE.equals(customerSettlementDetail.getStatus())){
				customerSettlementDetail.setCustomerSettlementId(customerSettlement);
				if (customerSettlementDetail.getIsNewRecord()){
					customerSettlementDetailDao.insert(customerSettlementDetail);
				}else{
					customerSettlementDetailDao.update(customerSettlementDetail);
				}
			}else{
				customerSettlementDetailDao.delete(customerSettlementDetail);
			}
		}
	}

	/**
	 * 更新状态
	 * @param customerSettlement
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(CustomerSettlement customerSettlement) {
		super.updateStatus(customerSettlement);
	}

	/**
	 * 删除数据
	 * @param customerSettlement
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(CustomerSettlement customerSettlement) {
		super.delete(customerSettlement);
		CustomerSettlementPayment customerSettlementPayment = new CustomerSettlementPayment();
		customerSettlementPayment.setCustomerSettlementId(customerSettlement);
		customerSettlementPaymentDao.deleteByEntity(customerSettlementPayment);
		CustomerSettlementPrepayment customerSettlementPrepayment = new CustomerSettlementPrepayment();
		customerSettlementPrepayment.setCustomerSettlementId(customerSettlement);
		customerSettlementPrepaymentDao.deleteByEntity(customerSettlementPrepayment);
		CustomerSettlementDetail customerSettlementDetail = new CustomerSettlementDetail();
		customerSettlementDetail.setCustomerSettlementId(customerSettlement);
		customerSettlementDetailDao.deleteByEntity(customerSettlementDetail);
	}

}
